package dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionFactory {
	private static ConnectionFactory singleton;
	private static String url;
	private static String usuario;
	private static String senha;
	private static String banco;
	protected static Connection conexao;

	/**
	 * Metodo usado para garantir a criacao de um unico objeto ConnectionFactory
	 * Design patterns Singleton
	 * 
	 * @return singleton
	 */
	public static ConnectionFactory getInstance() {
		if (singleton == null) {
			System.out.println("criando obj banco");
			singleton = new ConnectionFactory(1);
		} else {
			System.out.println("N�o duplica o obj banco");
		}
		return singleton;
	}
    //Construtor privado, s� pode ser chamado pelo metodo getInstace();
	private ConnectionFactory(int i) {
            
            if(i == 1){
                url = "jdbc:sqlserver://TIAGO-PC\\SQLEXPRESS;";
		banco = "VideoLocadora";
		usuario = "sa";
		senha = "SQL#2008";
                conecta();
		
            
            }else{
                url = "jdbc:sqlserver://TTI-181\\SQLEXPRESS:1433;";
		banco = "VIDEOLOCADORATTI";
		usuario = "sa";
		senha = "SQL#2008";
                conecta();
            }
		

	}

	private static void  conecta() {
		try {
                       
			String urlConexao = (url + "databaseName=" + banco + ";user="
					+ usuario + ";password=" + senha + ";");
                        System.out.println(urlConexao);
			conexao = DriverManager.getConnection(urlConexao);
                        
		} catch (Exception e) {
			System.out.println(e);
		}
           

	}
        protected static Connection getConexao(){
            return conexao;
        }

}
